package com.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.pojo.Jingdian;
import com.test.utils.JdbcUtils;
import com.test.utils.Msg;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class JingdianAllServlet
 * 前端按城市查询所有的景点
 */
public class JingdianAllServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JingdianAllServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("我进来了。。。。");
		
		response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String yiji = request.getParameter("yiji");  //省
        String erji = request.getParameter("erji");   //市
        String sanji = request.getParameter("sanji");  //县
        List<Jingdian> list = new ArrayList<Jingdian>();
        JdbcUtils jdbcUtils = new JdbcUtils();
        try {
			Connection conn = jdbcUtils.getConnection();
			Statement stmt = conn.createStatement();
			String sql = "select * from scenic_spot where yiji='"+yiji+"' and erji='"+erji+"' and sanji='"+sanji+"'";
			ResultSet result =  stmt.executeQuery(sql); //执行sql语句
			Jingdian jingdian = null;
			while(result.next()) {
				jingdian = new Jingdian();
				int id = result.getInt("id");
				String name = result.getString("name");
				String yiji1 = result.getString("yiji");
				String erji1 = result.getString("erji");
				String sanji1 = result.getString("sanji");
				String jianjie = result.getString("jianjie");
				String jianjietu = result.getString("jianjietu");
				String wenhua = result.getString("wenhua");
				String wenhuatu = result.getString("wenhuatu");
				String lishi = result.getString("lishi");
				String lishitu = result.getString("lishitu");
				String techang = result.getString("techang");
				String techangtu = result.getString("techangtu");
				jingdian.setErji(erji1);
				jingdian.setId(id);
				jingdian.setJianjie(jianjie);
				jingdian.setJianjietu(jianjietu);
				jingdian.setLishi(lishi);
				jingdian.setLishitu(lishitu);
				jingdian.setName(name);
				jingdian.setSanji(sanji1);
				jingdian.setTechang(techang);
				jingdian.setTechangtu(techangtu);
				jingdian.setWenhua(wenhua);
				jingdian.setWenhuatu(wenhuatu);
				jingdian.setYiji(yiji1);
				list.add(jingdian);
			}
		} catch (ClassNotFoundException | SQLException e) {
			
			e.printStackTrace();
		}
        
        out.println(JSONObject.fromObject(Msg.success().add("list", list)));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
